<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\ProjectEase;

/**
 * ProjectEaseSearch represents the model behind the search form about `backend\models\ProjectEase`.
 */
class ProjectEaseSearch extends ProjectEase
{
    public function attributes()
    {
        return array_merge(parent::attributes(), ['package.owner']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'package_id', 'province', 'city', 'category', 'is_highlights', 'is_suit', 'management_status', 'stakeholders_status', 'level', 'source', 'owner',], 'integer'],
            [['suit', 'execution', 'detail', 'note', 'follow',], 'string'],
            [['code', 'title', 'borrower', 'principal', 'interest', 'highlights', 'stakeholders', 'mortgage_principal', 'mortgage_interest', 'mortgage_owner', 'mortgage_type',
                'other_warrants_code', 'mortgage_address', 'mortgage_building_area', 'mortgage_land_area', 'mortgage_detail', 'created_at', 'package.owner', 'updated_at', 'tag'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ProjectEase::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ],
            ]
        ]);

        $query->joinWith(['package' => function ($query) {
            $query->from(['package' => 'package_ease']);
        }]);

//        if (Yii::$app->user->identity->superadmin or in_array(Yii::$app->user->identity->realname, Yii::$app->params['special_access_users'])) {
////        if (in_array(Yii::$app->user->identity->realname, Yii::$app->params['special_access_users'])) {
//
//        } else {
//            $same_department = BackendUsers::find()->where(['department' => Yii::$app->user->identity->department])->select('id')->indexBy('id')->asArray()->all();
//            $query->andWhere(['in', 'package.owner', array_keys($same_department)]);
//        }
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            '`project_ease`.level' => $this->level,
            'package_id' => $this->package_id,
            '`project_ease`.province' => $this->province,
            '`project_ease`.city' => $this->city,
            '`project_ease`.category' => $this->category,
            '`project_ease`.source' => $this->source,
            '`project_ease`.is_highlights' => $this->is_highlights,
            '`project_ease`.is_suit' => $this->is_suit,
            '`project_ease`.owner' => $this->owner,
            'management_status' => $this->management_status,
            'stakeholders_status' => $this->stakeholders_status,
            '`project_ease`.created_at' => $this->created_at,
            '`project_ease`.owner' => $this->owner,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'borrower', $this->borrower])
            ->andFilterWhere(['like', '`project_ease`.principal', $this->principal])
            ->andFilterWhere(['like', '`project_ease`.interest', $this->interest])
            ->andFilterWhere(['like', '`project_ease`.highlights', $this->highlights])
            ->andFilterWhere(['like', '`project_ease`.stakeholders', $this->stakeholders])
            ->andFilterWhere(['like', 'mortgage_principal', $this->mortgage_principal])
            ->andFilterWhere(['like', 'mortgage_interest', $this->mortgage_interest])
            ->andFilterWhere(['like', 'mortgage_owner', $this->mortgage_owner])
            ->andFilterWhere(['like', 'mortgage_type', $this->mortgage_type])
            ->andFilterWhere(['like', 'other_warrants_code', $this->other_warrants_code])
            ->andFilterWhere(['like', 'mortgage_address', $this->mortgage_address])
            ->andFilterWhere(['like', 'mortgage_building_area', $this->mortgage_building_area])
            ->andFilterWhere(['like', 'mortgage_land_area', $this->mortgage_land_area])
            ->andFilterWhere(['like', 'suit', $this->suit])
            ->andFilterWhere(['like', 'execution', $this->execution])
            ->andFilterWhere(['like', 'detail', $this->detail])
            ->andFilterWhere(['like', 'note', $this->note])
            ->andFilterWhere(['like', '`project_ease`.follow', $this->follow])
            ->andFilterWhere(['like', '`project_ease`.updated_at', $this->updated_at])
            ->andFilterWhere(['like', 'mortgage_detail', $this->mortgage_detail]);

        if ($this->tag) {
//            $this->tag = implode(',', $this->tag);
            $query->andWhere("find_in_set('" . $this->tag . "',`tag`)");
        }
        return $dataProvider;
    }
}
